package org.wuxinshui.boosters.designPatterns.singleton;

/**
 * 双重检查锁定单例
 * * 优点：延迟加载，线程安全，性能较好
 * * 缺点：实现稍复杂，需要volatile防止指令重排序
 */
public class DoubleCheckSingleton {
    private volatile DoubleCheckSingleton singleton = null;

    private DoubleCheckSingleton() {
        if (null != singleton) {
            throw new RuntimeException("单例模式不允许创建多个实例");
        }
    }

    public DoubleCheckSingleton getInstance() {
        if (null == singleton) {
            synchronized (DoubleCheckSingleton.class) {
                if (null == singleton) {
                    singleton = new DoubleCheckSingleton();
                }
            }
        }

        return singleton;
    }

}
